package com.ruima.erp.framework.rest;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.Iterator;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ruima.erp.framework.dao.IRmSysUser;
import com.ruima.erp.framework.db.impl.RmSysUserImpl;
import com.ruima.erp.framework.entity.RmSysUser;

@Path("RmSystemUserService")
public class RmSystemUserService {
	
	private static Log LOG = LogFactory.getLog(RmSystemUserService.class);
	
	private static String SYSTEM_USER_JSON;
	
	private IRmSysUser rmSysUserImpl = new RmSysUserImpl();

	@GET
	@Produces(MediaType.TEXT_HTML)
	@Path("getrmsystemuserlist")
	/**
	 * 
	 *  "{\"result\":0}";
	 */
	public String getRmSystemUserList(){
		try {
			if(SYSTEM_USER_JSON==null){
				JsonConfig jc = new JsonConfig();
				jc.setExcludes(new String[]{});
				SYSTEM_USER_JSON = JSONArray.fromObject(rmSysUserImpl.getRmSysUsers(),jc).toString();
			}
			return "{\"identifier\":\"objid\",\"label\":\"objid\",\"items\":"+SYSTEM_USER_JSON+"}";
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "{\"result\":1}";
	}
	
	

	@GET
	@Produces(MediaType.TEXT_HTML)
	@Path("getrmsystemuserlistbyid")
	/**
	 * 
	 *  "{\"result\":0}";
	 */
	public String getRmSystemUserListById(@QueryParam("id")String id){
		try {
			JsonConfig jc = new JsonConfig();
			jc.setExcludes(new String[]{});
			String items = JSONArray.fromObject(rmSysUserImpl.getRmSysUsers(),jc).toString();
			return "{\"identifier\":\"objid\",\"label\":\"objid\",\"items\":"+items+"}";
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "{\"result\":1}";
	}
	
	
	@POST
	@Path("addrmsystemuser")
	@Produces("application/json; charset=UTF-8")
	/**
	 * 例如:
	 * 	{"createtime":"","birthday":"2012-10-09","objid":"3","updatetime":"",
	 * 	"email":"xxxxxxxxxx","name":"xxxxxxxxx","descript":"","createuserObjid":"1",
	 * 	"code":"xxxxxxxxx","identificationId":"4107031981039283948","disindex":""}
	 * 
	 * "{\"result\":0}";
	 */
	public String addRmSystemUser(@QueryParam("input")String input){
		if(input == null){
			return  "{\"result\":0}";
		}
		LOG.info(String.format("addRmSystemUser : input[%s]",input));
		JSONObject jsonObject = JSONObject.fromObject(input);
		Iterator<String> itkeys = jsonObject.keys();
		Class c = RmSysUser.class;
		Object object = null;
		try {
			object = c.newInstance();
		} catch (InstantiationException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		}
		
		Method[] fs = c.getMethods();
		
		while(itkeys.hasNext()){
			String k = itkeys.next();
			for(Method f :fs){
				String met = f.getName();
				if(met.toUpperCase().contains(("set"+k).toUpperCase())||met.toUpperCase().contains(("is"+k).toUpperCase())){
					try {
						System.out.println(f.getName());
						f.invoke(object, jsonObject.get(k));
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						e.printStackTrace();
					}
					break;
				}
			}
		}
		if(object instanceof RmSysUser){
			RmSysUser rmSysUser = (RmSysUser)object;
			try {
				rmSysUserImpl.addRmSysUser(rmSysUser);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		System.out.println(JSONObject.fromObject(object).toString());
			
		
		return "{\"result\":1}";
	}
	
	@POST
	@Path("editrmsystemuser")
	@Produces("application/json; charset=UTF-8")
	/**
	 * 例如:
	 * 	{"createtime":"","birthday":"2012-10-09","objid":"3","updatetime":"",
	 * 	"email":"xxxxxxxxxx","name":"xxxxxxxxx","descript":"","createuserObjid":"1",
	 * 	"code":"xxxxxxxxx","identificationId":"4107031981039283948","disindex":""}
	 * 
	 * "{\"result\":0}";
	 */
	public String editRmSystemUser(@QueryParam("input")String input){
		if(input == null){
			return  "{\"result\":0}";
		}
		LOG.info(String.format("editRmSystemUser : input[%s]",input));
		
		JSONObject jsonObject = JSONObject.fromObject(input);
		Iterator<String> itkeys = jsonObject.keys();
		Class c = RmSysUser.class;
		Object object = null;
		try {
			object = c.newInstance();
		} catch (InstantiationException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		}
		
		Method[] fs = c.getMethods();
		
		while(itkeys.hasNext()){
			String k = itkeys.next();
			for(Method f :fs){
				String met = f.getName();
				if(met.toUpperCase().contains(("set"+k).toUpperCase())||met.toUpperCase().contains(("is"+k).toUpperCase())){
					try {
						System.out.println(f.getName());
						f.invoke(object, jsonObject.get(k));
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						e.printStackTrace();
					}
					break;
				}
			}
		}
		if(object instanceof RmSysUser){
			RmSysUser rmSysUser = (RmSysUser)object;
			try {
				rmSysUserImpl.editRmSysUser(rmSysUser);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		System.out.println(JSONObject.fromObject(object).toString());
			
		
		return "{\"result\":1}";
	}
	
	
	@POST
	@Path("deletermsystemuser")
	@Produces("application/json; charset=UTF-8")
	/**
	 * 
	 *  "{\"result\":0}";
	 */
	public String deleteRmSystemUser(@QueryParam("id")String id){
		if(id == null){
			return  "{\"result\":0}";
		}
		LOG.info(String.format("deleteRmSystemUser : objid[%s]",id));
		RmSysUser rmSysUser = new RmSysUser ();
		rmSysUser.setObjid(id);
		try {
			rmSysUserImpl.deleteRmSysUser(rmSysUser);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "{\"result\":1}";
	}
	
	public static void main(String[] args) {
		//new RmSystemUserService().getRmSystemUserList();
		RmSysUser ru = new RmSysUser ();
		ru.setObjid("3");
		ru.setName("xxxxxxxxx");
		ru.setCode("xxxxxxxxx");
		ru.setEmail("xxxxxxxxxx");
		ru.setBirthday("2012-10-09");
		ru.setIdentificationId("4107031981039283948");
		ru.setCreateuserObjid("1");
		System.out.println(JSONObject.fromObject(ru).toString());
	}
}
